package org.hepeng.workx.mybatis.util;

import org.apache.commons.lang3.StringUtils;
import org.hepeng.workx.jdbc.DataSourceInfo;

import java.util.Objects;
import java.util.Properties;

import static org.hepeng.workx.mybatis.MybatisConstant.*;
import static org.hepeng.workx.jdbc.DataSourceInfo.DataSourceOpsMode.*;

/**
 * @author he peng
 */
public class WorkXMybatisConfigUtils {

    public static boolean isEnableDataSourceRoute(Properties props) {
        if (Objects.isNull(props)) {
            return false;
        }
        return Boolean.valueOf(props.getProperty(ENABLE_DATASOURCE_ROUTE, "false"));
    }

    public static boolean isEnablePublishEvent(Properties props) {
        if (Objects.isNull(props)) {
            return false;
        }
        return Boolean.valueOf(props.getProperty(ENABLE_PUBLISH_EVENT, "false"));
    }

    public static DataSourceInfo.DataSourceOpsMode getDataSourceMode(Properties props) {
        if (Objects.isNull(props)) {
            return READ_AND_WRITE;
        }
        String mode = props.getProperty(DATASOURCE_OPS_MODE, READ_AND_WRITE.name());
        return DataSourceInfo.DataSourceOpsMode.valueOf(StringUtils.upperCase(mode));
    }

    public static String getEventConsumerPackages(Properties props) {
        if (Objects.isNull(props)) {
            return "";
        }
        return props.getProperty(EVENT_CONSUMER_PACKAGES);
    }

    public static String getDatabaseType(Properties props) {
        if (Objects.isNull(props)) {
            return "";
        }
        return props.getProperty(DATABASE_TYPE);
    }
}
